package com.gitee.wsl.compose.mvi.component.base

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.gitee.wsl.compose.mvi.merge.Mergeable
import com.gitee.wsl.compose.mvi.modifiable.presentable.PresentableModifier

/**
 * Represents a model for creating a composable.
 */
interface Presentable<Model> : Mergeable<Model> where Model : Presentable<Model> {
    /**
     * The [Modifier]s.
     */
    val modifier: PresentableModifier // TODO should modifiers be segregated between presentable and interactable rather than just using Modifier directly?

    /**
     * Creates a localized version of this presentation.
     */
    @Composable
    fun localized(): Model // TODO add composition local usage?
}

@Suppress("UNCHECKED_CAST")
abstract class Presenter<Model>(
    override val modifier: PresentableModifier
) : Presentable<Model> where Model : Presenter<Model> {

    override fun merge(other: Model?): Model = if (other == null || other === this) this as Model else safeMerge(other)

    protected abstract fun safeMerge(other: Model): Model

    @Composable
    override fun localized(): Model = this as Model
}